var app = getApp(), fetchApi = require("../../../../utils/httpPost.js"), common_Js = require("../../../../component/common.js")
import Dialog from '../../../../component/vant/dialog/dialog';
import Toast from '../../../../component/vant/toast/toast';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    pagesize:15,
    pageIndex: 1,
    nodes:[],
    recordList:[],
    hasData:false,
    loading: false,
    netWork: false,
    isLoading: true,
    device_id:0,
    beginDate:'',
    endDate:'',
    chooseRange:'',
    minDate: new Date(2023, 0, 1).getTime(),
    showCalendar:false,
    is_test:0,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    this.data.is_test = app.getStorageSync("is_test");
    this.setData({
      is_test: this.data.is_test
    })
    
    if(options.id){
      this.data.device_id = parseInt(options.id);
      this.onHttpInit()
    }
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpInit: function () {
    var that = this;
    fetchApi.httpPost('/device/range/result/record', {
      device_id:that.data.device_id,
      begin_date: that.data.beginDate,
      end_date: that.data.endDate,
      page_index:that.data.pageIndex,
      page_size:that.data.pagesize
    }, function (res) {
      console.log("传感器设备上报记录响应数据", res)
      if (res.data.code == 0) {
        if (res.data.totalcount > 0) {
          that.data.pageIndex = that.data.pageIndex + 1;
          var records = res.data.data.records;
          console.log('records',records)
          for (var index in records) {
            that.data.recordList.splice(that.data.recordList.length, 0, records[index]);
          }
          that.setData({
            hasData: true,
            recordList: that.data.recordList
          })
        }
      }
      that.setData({
         isLoading: false, loading: true, netWork: true,
      })
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    }, function (res) {
        that.setData({ hasData: false,  isLoading: false,  loading: true, netWork: true })
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
    })
  },
  onChangeCalendar(){
   this.setData({
     showCalendar: !this.data.showCalendar
   })
  },
  formatDate(date) {
   date = new Date(date);
   var month = date.getMonth() + 1 < 10 ? '0'+(date.getMonth() + 1) :date.getMonth() + 1;
   var day = date.getDate() < 10 ? '0'+(date.getDate()) :date.getDate();
   return `${date.getFullYear()}-${month}-${day}`;
 },
  onConfirmCalendar(event){
   const [start, end] = event.detail;
   this.data.beginDate = this.formatDate(start);
   this.data.endDate = this.formatDate(end);
   this.setData({
     showCalendar: !this.data.showCalendar,
     chooseRange: `${this.formatDate(start)}~${this.formatDate(end)}`,
   });
   this.refreshrecordList();
  },
  goRangeChart(e){
    var item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: '../chart/index?id=' + item.deviceId + '&num=' + item.deviceNum + '&code=' + item.dataCode,
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.refreshrecordList()
  },
  refreshrecordList:function(){
    this.setData({
      loading: false,
      netWork: false,
      isLoading: true,
      recordList:[]
    })
    this.data.pageIndex = 1
    this.onHttpInit()
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      isLoading: true
    })
    this.onHttpInit()
  },
})